package com.itheima.tanhua.server.controller;

import com.itheima.commons.holder.TokenHolderUserId;
import com.itheima.tanhua.autoconfig.template.OssTemplate;
import com.itheima.tanhua.dubbo.service.MovementsService;
import com.itheima.tanhua.model.dto.MovementsDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author H Monster
 */
@RestController
@RequestMapping("/movements")
public class MovementsController {
    @Autowired
    private OssTemplate ossTemplate;

    @DubboReference
    private MovementsService movementsService;

    @PostMapping
    public ResponseEntity publishMovements(MultipartFile[] imageContent, MovementsDto movementsDto) throws Exception {
        // 获取当前用户id
        String userId = TokenHolderUserId.get();

        List<String> url = new ArrayList<>();

        // 将文件上传至阿里云,获取请求地址,循环赋值
        for (MultipartFile multipartFile : imageContent) {
            String imageUrl = ossTemplate.upload(multipartFile.getOriginalFilename(), multipartFile.getInputStream());
            url.add(imageUrl);
        }
        System.out.println(url);

        // 赋值封装
        movementsDto.setMedias(url.toString().replace("[", "").replace("]", ""));
        movementsDto.setUserId(userId);
        System.out.println(url);

        movementsService.saveMovements(movementsDto);

        return ResponseEntity.ok("动态发布成功");
    }


}
